It seems like 4 months since I got a chance to 'expand my mind', rather than manically try and complete some paid work. But last week, the workload cooled off enough for me to spend some time with my favourite pastime, trying to make binaries out of PHP.
As I've pointed out a few times in the blog, making .exe's/binaries from PHP, for me is more about solving the desktop (and maybe the daemon issue), rather than solving the web problem. As PHP in it's current form solves that perfectly well. (unless you happen to be desperatly insecure in your job, in which case Perl can work quite well.)
I had seen The
BinaryPHP project quite a while back, and thought it was an interesting idea. Using c++ to represent the rather painfull little item called a variable in PHP, made alot of sense. - It also had managed to build a small library of some of the functions (the other real pain for PHP). And not only that, it could actually compile Hello world, and a few others.
However, it did have one major design flaw, it tried to just use PHP's tokenizer to do the conversion. Having already got some way (although not much further than Hello world) with PHPSharp, my look at a .NET compiler. I had realized pretty early that a Parser (phpJay) was required to understand the language structure, and a tree builder and many, many nodes (treecc) where needed to represent the language prior to generation (What is now BinaryPHP_Generator_Function).
At present, it builds a parse tree, then runs emilCpp on each of the function blocks (main being the global scope one). and adds code ($function->addCode()) to the function, registers variables
($function->registerVar($name), $function->registerTmpVar() ) and generates some pretty funky code.
The current state should be pretty visable as it compiles
helloworld2.php into
helloworld2.php.cpp. The nodes are generated by the
Parser.jay, and emited by
Nodes.emitCpp.tc.
Then compiled by
BinaryPHP_Compiler (and it's factory provider for
g++)